/*
 *
 *
 *
 */


#ifndef __H_MLN_REDIS_DATABASE_H__
#define __H_MLN_REDIS_DATABASE_H__

#include "redisconn.h"

/* Desc:
 *      connect redis db, init the db context;
 *
 */
int mln_redis_db_init(redis_conn_ctx *ctx, char *ip,int ip_type, unsigned short port);
int mln_redis_db_release(redis_conn_ctx *ctx);

/* Desc:
 *      try lock until timeout;
 *      only used when write global info, such as dbversion;
 *
 * Params:
 *      timeout: microseconds;
 */
int mln_redis_db_global_lock(redis_conn_ctx *ctx, int timeout);
int mln_redis_db_global_unlock(redis_conn_ctx *ctx);

/* Desc:
 *      lock will wait until timeout; 
 *      trylock will return immediately;
 *
 * Params:
 *      timeout: microseconds;
 */
int mln_redis_db_domain_lock(redis_conn_ctx *ctx, char *policy_name, char *domain_name, int timeout);
int mln_redis_db_domain_trylock(redis_conn_ctx *ctx, char *policy_name, char *domain_name);
int mln_redis_db_domain_unlock(redis_conn_ctx *ctx, char *policy_name, char *domain_name);


/*
 * Desc:    
 *          check the version hash exists. 
 *          If 'not exists' and 'redis with zero keys':
 *              global lock db;
 *              init it;
 *              global unlock db;
 *          else:
 *              return error;
 * 
 * Return:  
 *          0,success; -1, error
 */
int mln_redis_db_checkversion(redis_conn_ctx *ctx);


/*
 * Desc:    
 *          create a domain and return domain id. 
 * 
 * Return:  
 *          0,success; -1, error
 */
int mln_redis_db_domain_create(redis_conn_ctx *ctx, char *policy_name, char *domain_name);
/*
 * Desc:    
 *          refresh the dbid and init domain info
 * Return:  
 *          0,success; -1, error
 */
int mln_redis_db_domain_refresh(redis_conn_ctx *ctx, char *policy_name, char *domain_name);
/*
 * Desc:    
 *          if change charset:
 *              refresh the domain
 * Return:  
 *          0,success; -1, error
 */
int mln_redis_db_domain_edit(redis_conn_ctx *ctx, char *policy_name, char *domain_name);
int mln_redis_db_domain_delete(redis_conn_ctx *ctx, char *policy_name, char *domain_name);
int mln_redis_db_domain_getid(redis_conn_ctx *ctx, char *policy_name, char *domain_name);



int mln_redis_db_url_create(redis_conn_ctx *ctx, int dbid, char *url, int url_len);
int mln_redis_db_url_delete(redis_conn_ctx *ctx, int dbid, char *url, int url_len);
int mln_redis_db_url_refresh(redis_conn_ctx *ctx, int dbid, char *url, int url_len);
int mln_redis_db_url_getid(redis_conn_ctx *ctx, int dbid, char *url, int url_len);


int mln_redis_db_arg_create(redis_conn_ctx *ctx, int dbid, int urlid, char *argname, int argname_len);
int mln_redis_db_arg_delete(redis_conn_ctx *ctx, int dbid, int urlid, char *argname, int argname_len);
int mln_redis_db_arg_refresh(redis_conn_ctx *ctx, int dbid, int urlid, char *argname, int argname_len);
int mln_redis_db_arg_getid(redis_conn_ctx *ctx, int dbid, int urlid, char *argname, int argname_len);



#endif
